home *** CD-ROM | disk | FTP | other *** search
- /* mkfifo -- make fifo's (named pipes)
- Copyright (C) 1990 Free Software Foundation, Inc.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 1, or (at your option)
- any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
-
- /* Usage: mkfifo [-p] [-m mode] [+path] [+mode mode] path...
-
- Options:
- -p, +path Ensure that the given path(s) exist:
- Make any missing parent directories for each argument.
- Parent dirs default to umask modified by `u+wx'.
- Do not consider an argument fifo that already
- exists to be an error.
- -m, +mode mode Set the mode of created fifo's to `mode', which is
- symbolic as in chmod and uses the umask as a point of
- departure.
-
- David MacKenzie <djm@ai.mit.edu> */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include "system.h"
- #include "getopt.h"
- #include "modechange.h"
-
- #ifdef STDC_HEADERS
- #include <errno.h>
- #include <stdlib.h>
- #else
- extern int errno;
- #endif
-
- #ifndef _POSIX_SOURCE
- #define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
- #endif
-
- int make_path ();
- void error ();
- void strip_trailing_slashes ();
- void usage ();
-
- /* If nonzero, ensure that a path exists. */
- int path_mode;
-
- /* The name this program was run with. */
- char *program_name;
-
- struct option longopts[] =
- {
- {"mode", 1, NULL, 'm'},
- {"path", 0, &path_mode, 1},
- {NULL, 0, NULL, 0}
- };
-
- void
- main (argc, argv)
- int argc;
- char **argv;
- {
- unsigned short newmode;
- unsigned short parent_mode;
- struct mode_change *change;
- char *symbolic_mode;
- int errors = 0;
- int optc;
- int ind;
-
- program_name = argv[0];
- path_mode = 0;
- symbolic_mode = NULL;
-
- while ((optc = getopt_long (argc, argv, "pm:", longopts, &ind)) != EOF)
- {
- if (optc == 0 && longopts[ind].flag == 0)
- optc = longopts[ind].val;
- switch (optc)
- {
- case 0: /* Long option. */
- break;
- case 'p':
- path_mode = 1;
- break;
- case 'm':
- symbolic_mode = optarg;
- break;
- default:
- usage ();
- }
- }
-
- if (optind == argc)
- usage ();
-
- newmode = 0777 & ~umask (0);
- parent_mode = newmode | 0300; /* u+wx */
- newmode &= ~0111; /* Remove execute permission. */
- if (symbolic_mode)
- {
- change = mode_compile (symbolic_mode, MODE_MASK_EQUALS | MODE_MASK_PLUS);
- if (change == MODE_INVALID)
- error (1, 0, "invalid mode");
- else if (change == MODE_MEMORY_EXHAUSTED)
- error (1, 0, "virtual memory exhausted");
- newmode = mode_adjust (newmode, change);
- }
-
- for (; optind < argc; ++optind)
- {
- strip_trailing_slashes (argv[optind]);
- if (path_mode)
- errors |= make_path (argv[optind], newmode, parent_mode);
- else if (mkfifo (argv[optind], newmode))
- {
- error (0, errno, "cannot make fifo `%s'", argv[optind]);
- errors = 1;
- }
- }
-
- exit (errors);
- }
-
- /* Make sure fifo `path' and all leading directories exist,
- and give it permission mode `mode'.
- If any leading directories are created, give them permission
- mode `parent_mode'.
- Return 0 if successful, 1 if errors occur. */
-
- int
- make_path (path, mode, parent_mode)
- char *path;
- unsigned short mode;
- unsigned short parent_mode;
- {
- char *slash;
- struct stat stats;
-
- if (stat (path, &stats))
- {
- slash = path;
- while (*slash == '/')
- slash++;
- while (slash = index (slash, '/'))
- {
- *slash = 0;
- if (stat (path, &stats))
- {
- if (mkdir (path, parent_mode))
- {
- error (0, errno, "cannot make directory `%s'", path);
- return 1;
- }
- }
- else if ((stats.st_mode & S_IFMT) != S_IFDIR)
- {
- error (0, 0, "`%s' is not a directory", path);
- return 1;
- }
- *slash++ = '/';
- }
-
- if (mkfifo (path, mode))
- {
- error (0, errno, "cannot make fifo `%s'", path);
- return 1;
- }
- }
- else if ((stats.st_mode & S_IFMT) != S_IFIFO)
- {
- error (0, 0, "`%s' is not a fifo", path);
- return 1;
- }
- else if (chmod (path, mode))
- {
- error (0, errno, "cannot change mode of `%s'", path);
- return 1;
- }
- return 0;
- }
-
- /* Remove trailing slashes from PATH; they cause some system calls to fail. */
-
- void
- strip_trailing_slashes (path)
- char *path;
- {
- int last;
-
- last = strlen (path) - 1;
- while (last > 0 && path[last] == '/')
- path[last--] = '\0';
- }
-
- void
- usage ()
- {
- fprintf (stderr, "\
- Usage: %s [-p] [-m mode] [+path] [+mode mode] path...\n",
- program_name);
- exit (1);
- }
-
-